
function setlocalStorage (key,val) {
  localStorage.setItem(key,val)
}
function getlocalStorage (key) {
  let item = localStorage.getItem(+key);
  return item
}
function removelocalStorage (key) {
  localStorage.removeItem(key)
}
function checklocalStorage (key) {
  let userdata = getlocalStorage(key);
  if(!!userdata){
    try {
      userdata = JSON.parse(userdata)
      return userdata
    } catch (err){
      return false
    }
  }else{
    return false
  }

}
/*获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”*/
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  var Hours =  date.getHours();
  var Minutes =  date.getMinutes();
  var Seconds =  date.getSeconds();

  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  if (Hours >= 0 && Hours <= 9) {
    Hours = "0" + Hours;
  }
  if (Minutes >= 0 && Minutes <= 9) {
    Minutes = "0" + Minutes;
  }
  if (Seconds >= 0 && Seconds <= 9) {
    Seconds = "0" + Seconds;
  }

  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + " " + Hours + seperator2 + Minutes + seperator2 + Seconds;
  return currentdate;
}
/* 获取前后几个月日期
* @date 格式为yyyy-mm-dd的日期，如：2014-01-25
* @num number 
*/
function getPreMonth(date,num) {
  var arr = date.split('-');
  var year = arr[0]; //获取当前日期的年份
  var month = arr[1]; //获取当前日期的月份
  var day = arr[2]; //获取当前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //获取当前日期中月的天数
  var year2 = year;
  var month2 = parseInt(month) + num;
  if (month2 == 0) {
    year2 = parseInt(year2) - 1;
    month2 = 12;
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
    day2 = days2;
  }
  if (month2 < 10) {
    month2 = '0' + month2;
  }
  var t2 = year2 + '-' + month2 + '-' + day2;
  return t2;
}

let common ={
  setlocalStorage:setlocalStorage,
  getlocalStorage:getlocalStorage,
  removelocalStorage:removelocalStorage,
  checklocalStorage:checklocalStorage,
  getNowFormatDate:getNowFormatDate,
  getPreMonth:getPreMonth
}

// 返回在vue模板中的调用接口
export default {
  install (Vue) {
    Vue.prototype.$common = common
    Vue.common = common
  },
  common: common
}

export const $common = common
